package com.ruoyi.mp.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 公众号粉丝对象 mp_user
 *
 * @author ruoyi
 * @date 2023-11-18
 */
public class MpUser extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 编号 */
    private Long id;

    /** 用户标识 */
    @Excel(name = "用户标识")
    private String openid;

    /** 关注状态 */
    @Excel(name = "关注状态")
    private Integer subscribeStatus;

    /** 关注时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "关注时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date subscribeTime;

    /** 昵称 */
    @Excel(name = "昵称")
    private String nickname;

    /** 头像地址 */
    @Excel(name = "头像地址")
    private String headImageUrl;

    /** 取消关注时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "取消关注时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date unsubscribeTime;

    /** 语言 */
    @Excel(name = "语言")
    private String language;

    /** 国家 */
    @Excel(name = "国家")
    private String country;

    /** 省份 */
    @Excel(name = "省份")
    private String province;

    /** 城市 */
    @Excel(name = "城市")
    private String city;

    /** 标签编号数组 */
    @Excel(name = "标签编号数组")
    private String tagIds;
    /** 微信公众号ID */
    @Excel(name = "微信公众号ID")
    private Long accountId;

    /** 微信公众号 appid */
    @Excel(name = "微信公众号 appid")
    private String appId;


    /** 是否删除 */
    @Excel(name = "是否删除")
    private String deleted;

    /** 租户编号 */
    @Excel(name = "租户编号")
    private Long tenantId;

    /** 关注渠道来源 */
    @Excel(name = "关注渠道来源")
    private String subscribeScene;
    public void setId(Long id)
    {
        this.id = id;
    }

    public Long getId()
    {
        return id;
    }
    public void setOpenid(String openid)
    {
        this.openid = openid;
    }

    public String getOpenid()
    {
        return openid;
    }
    public void setSubscribeStatus(Integer subscribeStatus)
    {
        this.subscribeStatus = subscribeStatus;
    }

    public Integer getSubscribeStatus()
    {
        return subscribeStatus;
    }
    public void setSubscribeTime(Date subscribeTime)
    {
        this.subscribeTime = subscribeTime;
    }

    public Date getSubscribeTime()
    {
        return subscribeTime;
    }
    public void setNickname(String nickname)
    {
        this.nickname = nickname;
    }

    public String getNickname()
    {
        return nickname;
    }
    public void setHeadImageUrl(String headImageUrl)
    {
        this.headImageUrl = headImageUrl;
    }

    public String getHeadImageUrl()
    {
        return headImageUrl;
    }
    public void setUnsubscribeTime(Date unsubscribeTime)
    {
        this.unsubscribeTime = unsubscribeTime;
    }

    public Date getUnsubscribeTime()
    {
        return unsubscribeTime;
    }
    public void setLanguage(String language)
    {
        this.language = language;
    }

    public String getLanguage()
    {
        return language;
    }
    public void setCountry(String country)
    {
        this.country = country;
    }

    public String getCountry()
    {
        return country;
    }
    public void setProvince(String province)
    {
        this.province = province;
    }

    public String getProvince()
    {
        return province;
    }
    public void setCity(String city)
    {
        this.city = city;
    }

    public String getCity()
    {
        return city;
    }
    public void setTagIds(String tagIds)
    {
        this.tagIds = tagIds;
    }

    public String getTagIds()
    {
        return tagIds;
    }
    public void setAccountId(Long accountId)
    {
        this.accountId = accountId;
    }

    public Long getAccountId()
    {
        return accountId;
    }
    public void setAppId(String appId)
    {
        this.appId = appId;
    }

    public String getAppId()
    {
        return appId;
    }
    public void setDeleted(String deleted)
    {
        this.deleted = deleted;
    }

    public String getDeleted()
    {
        return deleted;
    }
    public void setTenantId(Long tenantId)
    {
        this.tenantId = tenantId;
    }

    public Long getTenantId()
    {
        return tenantId;
    }

    public String getSubscribeScene() {
        return subscribeScene;
    }

    public void setSubscribeScene(String subscribeScene) {
        this.subscribeScene = subscribeScene;
    }

    public MpUser changeWxMpUser(WxMpUser wxMpUser, String appId) throws ParseException {

        setOpenid(wxMpUser.getOpenId());

        Long time = Long.parseLong(String.valueOf(wxMpUser.getSubscribeTime()));
        ThreadLocal<SimpleDateFormat> sdfYMDHMS = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        ThreadLocal<SimpleDateFormat> sdfYMD = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

        String subtime = sdfYMDHMS.get().format(new Date(time * 1000L));
        try {
            Date date = sdfYMD.get().parse(subtime);
            setSubscribeTime(date);
        } catch (ParseException e) {
            throw e;
        }


        StringBuilder tags = new StringBuilder();
        for (int i = 0; i < wxMpUser.getTagIds().length; i++) {
            tags.append(wxMpUser.getTagIds()[i]);
            if (i < wxMpUser.getTagIds().length - 1) {
                tags.append(",");
            }
        }
        String tagString = tags.toString();

        int intValue = wxMpUser.getSubscribe() ? 1 : 0;
        setSubscribeStatus(intValue);
        setTagIds(tagString);
        setNickname(wxMpUser.getNickname());
        setHeadImageUrl(wxMpUser.getHeadImgUrl());
        setSubscribeScene(wxMpUser.getSubscribeScene());
        return this;
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("openid", getOpenid())
            .append("subscribeStatus", getSubscribeStatus())
            .append("subscribeTime", getSubscribeTime())
            .append("nickname", getNickname())
            .append("headImageUrl", getHeadImageUrl())
            .append("unsubscribeTime", getUnsubscribeTime())
            .append("language", getLanguage())
            .append("country", getCountry())
            .append("province", getProvince())
            .append("city", getCity())
            .append("remark", getRemark())
            .append("tagIds", getTagIds())
            .append("accountId", getAccountId())
            .append("appId", getAppId())
            .append("createTime", getCreateTime())
            .append("updateTime", getUpdateTime())
            .append("deleted", getDeleted())
            .append("tenantId", getTenantId())
            .toString();
    }
}
